<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <style>
      div {
        width: 80px;
      }

      input[type='text'] {
        width: 50px;
        height: 44px;
        outline: none;
        border: 1px solid #ccc;
        text-align: center;
        border-right: 0;
      }

      input[type='button'] {
        height: 24px;
        width: 22px;
        cursor: pointer;
      }

      input {
        float: left;
        border: 1px solid #ccc;
      }
    </style>
  </head>

  <body>
    <div>
      <input type="text" id="total" value="1" readonly />
      <input type="button" value="+" id="add" />
      <input type="button" value="-" id="reduce" disabled />
      <script>
        // 1. 获取元素  三个
        const text = document.querySelector('#total')
        // console.log(text.value);
        // console.log(text);
        const add = document.querySelector('#add')
        const reduce = document.getElementById('reduce')
        // 定义数字计数
        let count = 1
        // 封装禁止点击方法
        function disabled(){
          if(text.value > 0){
          reduce.disabled = false
        }else(
          reduce.disabled = true
        )
        }
        disabled()
        // 给加和减绑定点击事件
        add.addEventListener('click',function(){
          count++
          text.value = count
          disabled()
        })
        // console.log(text.value);
        reduce.addEventListener('click',function(){
          count--
          text.value = count
          disabled()
        })
        
      </script>
    </div>
  </body>
</html>
